/**
 * Created by zhai shuai shuai on 2017/6/29.
 */


//下面是下载
var formatter = function (value, row, index) {
    return ['<a href="javascript:void(0)" onclick="downloadFile(\''+value+'\')"><span style="color:blue;">下载</span></a>'];
}

var formatterOper =function(value,row,index){
    if(role==1){
        return ['<a href="javascript:void(0)" class="detail">查看</a>&nbsp&nbsp&nbsp'+'<a herf="javascript:void(0)"' +
        ' class="alterpro">修改</a>'];
    }else{
        return ['<a href="javascript:void(0)" class="detail">查看</a>'];
    }


}

var downloadFile = function (path) {

    if ("" != path) {
        var a = document.createElement("a");
        a.href = File_Download_Url + path;

        var filePathArray = path.split('\/');
        var filename  = filePathArray[filePathArray.length-1];
        a.download = filename;
        a.target = "_blank";
        a.click();
    } else {
        hint("未上传文件")
    }
    return false;

}


window.operateEvents = {

    'click .detail':function(e,value, row, index){

                showModel("slidingmenu", function () {
                    // if (row.auditState == '等待审核') {
                    //     $("#okBtn").show();
                    //     $("#noBtn").show();
                    // }
                    setRowToFrm("detailProForm", row);

                    if("是"==row.isRegEnterprice){
                        $(".isHidd").show();
                    }

                    if (row.videoPath != null && row.videoPath != '') {
                        $("#clickVideo").show();
                    } else {
                        $("#clickVideo").hide();
                    }

                    $("#downloadDiv").html("");

                    if (row.attachmentPath != null) {
                        if (row.attachmentPath.indexOf(",") != -1) {
                            var pathArray = row.attachmentPath.split(",");
                            var btnButton = "";
                            for (var i = 0; i < pathArray.length; i++) {
                                var filenameArray = pathArray[i].split('\/');
                                var filename = filenameArray[filenameArray.length - 1];
                                btnButton = btnButton + "<button class='fileBtn' data-value='" + pathArray[i] + "'>" + (i + 1) + '. ' + filename + " - 点击下载</button><br/><br/>";
                            }
                            $("#downloadDiv").html(btnButton);
                        } else {
                            var filenameArray = row.attachmentPath.split('\/');
                            var filename = filenameArray[filenameArray.length - 1];
                            $("#downloadDiv").html("<button class='fileBtn' data-value='" + row.attachmentPath + "'>" + filename + " - 点击下载</button>");
                        }

                        $("#downloadDiv").find(".fileBtn").click(function () {
                            var filePath = $(this).data("value");
                            if ("" != filePath) {
                                var a = document.createElement("a");
                                a.href = File_Download_Url + filePath;
                                var filePathArray = filePath.split('\/');
                                var filename = filePathArray[filePathArray.length - 1];
                                a.download = filename;
                                a.target = "_blank";
                                a.click();
                            } else {
                                hint("未上传商业计划书");
                            }
                            return false;
                        });
                    }

                });
    },

    //下面是显示修改信息
    'click .alterpro':function(e,value, row, index){
        showModel("slidingmenuAlter",function(){
            setRowToFrm("alterDetail",row)
        });
    }
}

//下面是点击审核通过
bindClick("#okBtn",function(){
    //获取下面的参数
    var id = $("#id").val();
    var mobile = $("#mobile").val();
    var reason = $.trim($("#auditreason").val());
    var email = $("#proposerEmail").val();
    var proposer = $("#proposer").val();
    var projectName = $("#projectName").val();

    comment.showOperationTips("确认审核通过吗？", function () {
        $.post($().AbsUrlParse('/web/userproject/auditState2'), {
            id: id,
            mobile:mobile,
            state: '审核通过',
            reason: reason,
            email:email,
            proposer:proposer,
            projectName:projectName
        }, function (data) {
            if (data.state == true) {
                if (data.res.code == 40000) {
                    closeModel("finddetail-div");
                    hint("申报项目已审核成功并发送了邮件与短信");
                    var table = $('#table');
                    table.bootstrapTable('refresh');
                } else {
                    hint(data.res.msg);
                }
            } else {
                hint(data.msg);
            }
        });
    });
    closeModel("slidingmenu");
});

//下面是审核失败
bindClick("#noBtn",function(){
    //获取下面的参数
    var id = $("#id").val();
    var mobile = $("#mobile").val();
    var email = $("#proposerEmail").val();
    var proposer = $("#proposer").val();
    var reason = $.trim($("#auditreason").val());
    var projectName = $("#projectName").val();

    if ("" == reason) {
        hint("请填写审核失败原因");
        return false;
    }

    comment.showOperationTips("确认审核失败吗？",function(){
        $.post($().AbsUrlParse('/web/userproject/auditState2'),{
            id: id,
            mobile: mobile,
            state: '审核失败',
            reason: reason,
            proposer:proposer,
            email:email,
            projectName:projectName

        }, function(data){
            if(data.state==true){
                if(data.res.code==40000){
                    closeModel("finddetail-div");
                    hint("申报项目已更改为审核失败并发送了邮件及短信");
                    var table = $('#table');
                    table.bootstrapTable('refresh');
                }else{
                    hint(data.res.msg);
                }
            }else{
                hint(data.msg);
            }
        });
    });
    closeModel("slidingmenu");
});




$.post($().AbsUrlParse("/web/park/findParks"),{

 },function(data){

    var datas = data.res.data;
    var selects = "<select class='form-control' name='parkName'><option value=''>---请选择园区---</option>";
    for(var i=0;i<datas.length;i++){

        var val = datas[i].parkName;
        selects+= "<option name="+val+">"+val+"</option>"
    }
    selects+="</select>"
    $("#parkSelect").html(selects);
});



//下面是查询
bindClick("#btnSearch",function(){
    var parkName = $("select[name=parkName]").val();
    var hangYe = $("select[name=hangYe]").val();
    var auditState = $("select[name=auditState]").val();

    var mutilSearch = $("input[name=mutilSearch]").val();
    var url = $().AbsUrlParse("/web/userproject/queryDataAudit?parkName="+parkName+"&hangYe="+hangYe+"&auditState="+auditState+"&mutilSearch="+mutilSearch);
    $("#table").bootstrapTable("refresh", {"url": url});
});

bindClick("#downloadBtn", function () {
    var filePath = $("input[name=attachmentPath]").val();

    if ("" != filePath) {
        var a = document.createElement("a");
        a.href = File_Download_Url + filePath;
        var filePathArray = filePath.split('\/');
        var filename = filePathArray[filePathArray.length - 1];
        a.download = filename;
        a.target = "_blank";
        a.click();
    } else {
        hint("未上传商业计划书")
    }

    return false;
});


bindClick("#downloadBtnOne", function () {
    var filePath = $("input[name=attachmentPath]").val();
    if ("" != filePath) {
        var a = document.createElement("a");
        a.href = File_Download_Url + filePath;
        a.target = "_blank";
        a.click();
    } else {
        hint("未上传商业计划书")
    }
    return false;
});



//下面是修改的确认按钮
bindClick("#okAlter",function(){
    var form = $("#alterDetail");
    var params = form.formToArray();
    comment.post(form.attr("action"), params, '执行保存', function (res) {
        if (res.res.code == 40000) {
            //关闭model方法
            closeModel("slidingmenuAlter");
            var table = $("#table");
            table.bootstrapTable("refresh");
        }else{
            hint(res.res.msg);
        }
    });
});

bindClick("#clickVideo", function () {
    var filePath = $("input[name=videoPath]").val();
    if ("" != filePath) {

        var a = document.createElement("a");
        a.href = filePath;
        a.target = "_blank";
        a.click();
    } else {
        hint("未上传宣传视频")
    }
    return false;
});